// formPackage/pages/article/selectArticleHouse/selectArticleHouse.js

import { queryArticleHouse } from "../../../../service/article_service"
import {
  queryNewHouseByCondition,
} from "../../../../service/house_service"
import debounce from "../../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    switchArr: [
      {
        title: "关注楼盘",
        key: "",
      },
    ],
    houseList: [

    ],
    requestData: {
      cityName: "成都市",
      houseStyle: "",
      maxArea: null,
      maxTotalPrice: null,
      maxUnitPrice: null,
      minArea: null,
      minTotalPrice: null,
      minUnitPrice: null,
      page: 0,
      pageSize: 20,
      saleStatus: "",
      subway: "",
      tags: [],
      words: "",
      zone: ""
    },
    searchWord: "",
    topTypeIndex: 0,
    searchWords: "",
    selectHouseData: {
      houseNumber: "",
      name: ""
    },
    page: 0,
    pageSize: 100,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryArticleHouse();
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },

  //选择房源:
  houseSelectAction(e) {
    let index = e.currentTarget.dataset.index;
    let arr = this.data.houseList;
    arr.forEach((item, idx) => {
      item.isChoose = false;
      if (idx == index) {
        item.isChoose = true
        this.setData({
          'selectHouseData.name': item.name,
          'selectHouseData.houseNumber': item.houseNumber
        })
      }
    })
    this.setData({
      houseList: arr
    })
  },
  //搜索回调
  searchAction(e) {
    // console.log("JJLog:搜索回调:" + JSON.stringify(e));
    this.setData({
      searchWord: e.detail.searchWord,
      'requestData.words': e.detail.searchWord,
    })

    debounce(this.queryNewHouseByCondition);
  },



  //查询关注列表:
  queryArticleHouse() {
    wx.showLoading();
    queryArticleHouse(this.data.page, this.data.pageSize, this.data.searchWords).then(res => {
      wx.hideLoading();
      console.log("JJLog:关注列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        if (res.data && res.data.length > 0) {
          this.dealHouseList(res.data);
        }
      }
    }).catch(err => {
      wx.hideLoading();
      console.error("JJLog:queryArticleHouse", err);
    })
  },
  dealHouseList(arr) {
    arr.forEach((i) => {
      i.isChoose = false;
    })
    this.setData({
      houseList: arr
    })
  },
  //搜索房源:
  queryNewHouseByCondition() {
    wx.showLoading();
    queryNewHouseByCondition(this.data.requestData).then(res => {
      wx.hideLoading();
      console.log("JJLog:搜索结果:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          houseList: res.data || []
        })
        if (this.data.houseList.length > 0) {
          this.dealHouseList(res.data);
        }
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "搜索失败",
        icon: "none"
      })
      console.error("JJLog:queryNewHouseByCondition", err);
    })
  },
  //确认选择楼盘
  sureAction() {
    if (!this.data.selectHouseData.name) {
      wx.showToast({
        title: '请选择一个楼盘',
        icon: "none"
      })
      return;
    }
    app.globalData.articleHouse = this.data.selectHouseData;
    wx.navigateBack({
      delta: 0,
    })

  },



})